function clearToken() {
    localStorage.removeItem('tokrn')
    location.href = './login.html'
}

function getUserinfo() {
    $.ajax({
        url: '/my/userinfo',
        success: function (res) {
            console.log(res);
            if (res.code !== 0) return
            let name = res.data.nickname || res.data.username
            if (res.data.user_pic) {
                $('img.avatar').attr('src', res.data.user_pic).show()
                $('.text-avatar').hide()
            } else {
                $('img.avatar').hide()
                $('.text-avatar').show().html(name.charAt(0).toUpperCase())
            }
            $('.welcome').html('欢迎 ' + name)
        },
        complete: function (xhr) {
            if (xhr.status !== 200) {
                clearToken()
            }
        }
    })
}

getUserinfo()

$('.log-out').on('click', function () {
    layui.layer.confirm('是否退出登录？', {}, function (index) {
        clearToken()
        layui.layer.close(index)
    })
})